package com.solming.leetcode.question;

import java.util.LinkedList;
import java.util.Queue;

public class MyStack225 {

    Queue<Integer> queue;
    int top = 0;

    public MyStack225() {
        queue = new LinkedList<>();

    }

    /**
     * 添加元素到栈顶
     */
    public void push(int x) {
        top = x;
        queue.offer(x);
    }

    /**
     * 删除栈顶的元素并返回
     */
    public int pop() {
        int len = queue.size();
        while (len > 2) {
            queue.offer(queue.poll());
            len--;
        }
        //找到此时的下个元素
        top = queue.peek();
        queue.offer(queue.poll());
        return queue.poll();
    }


    /**
     * 返回栈顶元素
     */
    public int top() {
        return top;
    }

    /**
     * 判断栈是否为空
     */
    public boolean empty() {
        return queue.isEmpty();
    }

    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);

        int size = queue.size();
        while (size > 1) {
            int num = queue.poll();
            queue.offer(num);
            size--;
        }
        // 之前的队尾元素已经到了队头
        System.out.println(queue.poll());
    }
}
